package dev.alm.guiguxiaozhiapp.tools;

import dev.alm.guiguxiaozhiapp.bean.Appointment;
import dev.alm.guiguxiaozhiapp.service.AppointmentService;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AppointmentTools {

    @Resource
    private AppointmentService appointmentService;

    @Tool(name = "预约挂号", value = "根据参数，先查询是否有号源可以预约（调用queryDepartment方法），根据结果告知用户能否预约。如不能预约则直接返回用户。如可预约，则让用户确认所有预约信息。如果用户已经提供了所有预约信息，则直接帮用户预约。如果用户未提供预约医生的信息，则从向量数据库中找寻一位医生并分配")
    public String booking(Appointment appointment) {
        System.out.println("进入预约tool");
        // 先查找是否已经预约过了
        Appointment one = appointmentService.findOne(appointment);
        if (one != null) {
            return "用户已经预约过了。";
        }
        // 防止大模型幻觉设置id
        appointment.setId(null);
        Boolean save = appointmentService.save(appointment);
        if (!save) {
            return "预约失败！";
        }

        return "预约成功！";
    }

    @Tool(name = "根据用户姓名查询预约信息", value = "根据用户的姓名查询预约信息")
    public String queryBooking(@P("姓名") String username) {
        System.out.println("进入查询预约tool");
        List<Appointment> appointments = appointmentService.queryByUsername(username);
        if (appointments.size() == 0) {
            return "用户没有预约过。";
        }
        return appointments.toString();
    }

    @Tool(name = "取消预约挂号", value = "根据用户信息取消预约挂号结果")
    public String cancelBooking(Appointment appointment) {
        System.out.println("进入取消预约tool");
        Appointment one = appointmentService.findOne(appointment);
        if (one == null) {
            return "用户没有预约过。";
        }
        Boolean delete = appointmentService.delete(one.getId());
        if (!delete) {
            return "取消预约失败！";
        }
        return "取消预约成功！";
    }

    @Tool(name = "查询是否有号源", value = "根据科室名称、预约日期、预约时间和医生姓名查询是否有号源，将结果返回给用户")
    public boolean queryDepartment(
            @P(value = "科室名称") String departmentName,
            @P(value = "预约日期") String date,
            @P(value = "预约时间，可选值：上午、下午") String time,
            @P(value = "医生姓名", required = false) String doctorName
    ) {
        System.out.println("进入查询是否有号源tool");
        System.out.println("查询是否可预约");
        System.out.println("查询科室：" + departmentName);
        System.out.println("预约日期：" + date);
        System.out.println("预约时间：" + time);
        System.out.println("医生姓名：" + doctorName);
        // 先模拟，假设一直有号源
        return true;
    }

}
